package com.xunua.networkproject.Retrofit;

import android.os.Build;

import com.xunua.networkproject.Constant;
import com.xunua.networkproject.RetrofitBean.CommentBean;
import com.xunua.networkproject.RetrofitBean.CommentResultBean;
import com.xunua.networkproject.RetrofitBean.PostFileResultBean;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @user XUNUA
 * @email xunua@outlook.com
 * @creat time 2020/5/7 9:40
 */
public class RetrofitServiceImp {
    private static String baseUrl= Constant.baseUrl_wifi;//同局域网中手机使用的IP地址   ip是电脑的内网ip



    public static Call<String> getText(){
        final Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(new Converter.Factory() {//转换器  转换工厂
                    @Override
                    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                        return new Converter<ResponseBody, String>() {
                            @Override
                            public String convert(ResponseBody value) throws IOException {
                                return value.string();
                            }
                        };
                    }
                })
                .baseUrl(baseUrl)
                .build();
        Call<String> call = retrofit.create(RetrofitService.class).getText();
        return call;
    }

    public static Call<ParamBean> getParam(String keyword,String page,int order){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ParamBean> call = retrofit.create(RetrofitService.class).getParam(keyword, page, order);
        return call;
    }

    public static Call<ParamBean> getParam(Map<String,Object> map){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ParamBean> call = retrofit.create(RetrofitService.class).getParam(map);
        return call;
    }

    public static Call<String> postString(Map<String,Object> map){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(new Converter.Factory() {
                    @Override
                    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                        return new Converter<ResponseBody, String>() {
                            @Override
                            public String convert(ResponseBody value) throws IOException {
                                return value.string();
                            }
                        };
                    }
                })
                .build();
        Call<String> call = retrofit.create(RetrofitService.class).postString(map);
        return call;
    }

    public static Call<String> postString(String url){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(new Converter.Factory() {
                    @Override
                    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                        return new Converter<ResponseBody, String>() {
                            @Override
                            public String convert(ResponseBody value) throws IOException {
                                return value.string();
                            }
                        };
                    }
                })
                .build();
        Call<String> call = retrofit.create(RetrofitService.class).postString(url);
        return call;
    }


    public static Call<CommentResultBean> postComment(CommentBean commentBean){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<CommentResultBean> call = retrofit.create(RetrofitService.class).postComment(commentBean);
        return call;
    }

    public static Call<PostFileResultBean> postFile(MultipartBody.Part part,String token){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<PostFileResultBean> call = retrofit.create(RetrofitService.class).postFile(part,token);
        return call;
    }
    public static Call<PostFileResultBean> postFiles(List<MultipartBody.Part> parts){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<PostFileResultBean> call = retrofit.create(RetrofitService.class).postFiles(parts);
        return call;
    }

    public static Call<PostFileResultBean> postFileParams(MultipartBody.Part part,Map<String,String> params){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HashMap<String, String> headrmap = new HashMap<>();
        headrmap.put("key","xunua12312312313");
        headrmap.put("client","android"+ Build.VERSION.SDK_INT);
        Call<PostFileResultBean> call = retrofit.create(RetrofitService.class).postFileParams(part, params,headrmap);
        return call;
    }

    public static Call<PostFileResultBean> postLogin(String name,String pwd){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        Call<PostFileResultBean> call = retrofit.create(RetrofitService.class).postLogin(name, pwd);
        return call;
    };

    public static Call<ResponseBody> download(String url){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ResponseBody> call = retrofit.create(RetrofitService.class).download(url);
        return call;
    }






























    public static class ParamBean{

        /**
         * success : true
         * code : 10000
         * message : get带参数请求成功.
         * data : {"page":"1","keyword":"你好呀","order":"逆序"}
         */

        private boolean success;
        private int code;
        private String message;
        private DataBean data;

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public DataBean getData() {
            return data;
        }

        public void setData(DataBean data) {
            this.data = data;
        }

        public static class DataBean {
            /**
             * page : 1
             * keyword : 你好呀
             * order : 逆序
             */

            private String page;
            private String keyword;
            private String order;

            public String getPage() {
                return page;
            }

            public void setPage(String page) {
                this.page = page;
            }

            public String getKeyword() {
                return keyword;
            }

            public void setKeyword(String keyword) {
                this.keyword = keyword;
            }

            public String getOrder() {
                return order;
            }

            public void setOrder(String order) {
                this.order = order;
            }
        }
    }
}
